const {
  getallcollectessayid,
  register,
  login,
  getallusers,
  accomplish,
  updateuser,
  getoneuser,
  giteelogin,
  bygitee,
  mangelogin,
  deleteone,
} = require("../services/user.services");
const jwt = require("jsonwebtoken");
const fs = require("fs");
const path = require("path");
const axios = require("axios");
const privatekey = fs.readFileSync(
  path.resolve(__dirname, "../app/private.key")
);
class UserController {
  //查询当前用户收藏的所有文章的id，点赞的所有评论的id，点赞的所有留言的id
  async getallcollectessayid(ctx, next) {
    const { id } = ctx.request.params;
    try {
      ctx.body = await getallcollectessayid(id);
    } catch (e) {
      console.log(e);
    }
  }
  //注册
  async register(ctx, next) {
    const body = ctx.request.body;
    try {
      ctx.body = await register(body);
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }
  //登录
  async login(ctx, next) {
    const body = ctx.request.body;
    try {
      const result = await login(body);
      if (result.status === 401) return (ctx.body = result);
      const token = jwt.sign(result.user, privatekey, {
        algorithm: "RS256",
        expiresIn: 60 * 60,
      });
      ctx.body = {
        ...result,
        token,
      };
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //获取所有用户信息（包含搜索）
  async getallusers(ctx) {
    const { start, size, search } = ctx.request.query;
    try {
      let result;
      result = await getallusers(start || -1, size || -1, search || "");
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //用户个人成就
  async accomplish(ctx) {
    const { id } = ctx.request.params;
    try {
      const result = await accomplish(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //更新用户信息
  async updateuser(ctx) {
    const body = ctx.request.body;
    const id = ctx.request.params.id;
    try {
      const result = await updateuser(id, body);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //根据id重新获取用户信息
  async getoneuser(ctx) {
    const id = ctx.request.params.id;
    try {
      const result = await getoneuser(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //gitee登录
  async giteelogin(ctx) {
    let code = ctx.query.code || null;
    let client_id =
      "8bfb80fe67e8b3460d1233a3407f73506dead1f0d7634b768ad2b2e6728bab38";
    let client_secret =
      "61231ec9e9f738ce471f8ac109b0e25c05bb3c354aa930e868e9e06d47b5bf65";
    let redirect_uri = "http://localhost:8000/user/giteelogin";
    if (code) {
      let option1 = {
        method: "post",
        url: `https://gitee.com/oauth/token`,
        data: {
          grant_type: "authorization_code",
          code,
          client_id,
          client_secret,
          redirect_uri,
        },
      };

      const re1 = await axios(option1);
      const data1 = re1.data;

      let option2 = {
        method: "get",
        url: `https://gitee.com/api/v5/user`,
        params: {
          access_token: data1.access_token,
        },
      };

      const re2 = await axios(option2);
      await giteelogin(re2.data);
      //重定向回首页，并传回三方登录的id
      return ctx.redirect(
        `http://localhost:3000/#/homepage?gitee=${re2.data.id}`
      );
    }
    ctx.body = {
      status: 401,
      result: {
        message: "登录失败",
      },
    };
  }

  //根据giteeid获取用户信息
  async bygitee(ctx) {
    const id = ctx.request.params.id;
    try {
      const result = await bygitee(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //管理员登录
  async mangelogin(ctx) {
    const body = ctx.request.body;
    try {
      const result = await mangelogin(body);
      if (result.status === 401) return (ctx.body = result);
      const token = jwt.sign(result.result.user, privatekey, {
        algorithm: "RS256",
        expiresIn: 60 * 60,
      });
      ctx.body = {
        ...result,
        token,
      };
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }

  //删除用户
  async deleteone(ctx) {
    const id = ctx.request.params.id;
    try {
      const result = await deleteone(id);
      ctx.body = result;
    } catch (e) {
      console.log(e);
      ctx.body = e.message;
    }
  }
}

module.exports = new UserController();
